#include<stdio.h>
#include<malloc.h>
#define Maxsize 100
typedef char ElemType;
typedef struct Node{
    ElemType data;
    struct Node * left;
    struct Node * right;
}TNode;
void CreatTree(TNode *&b,char *str)
{   TNode *St[Maxsize],*p;
    int i=-1;k,j=0;char ch;
    b=null;
    ch=str[j];
    while(ch!='\0')
    {
        switch(ch)
        {
            case'(':top++;St[top]=p;k=1;break;
            case')':top--;break;
            case',':k=2;break;
            default:p=(TNode *)malloc(sizeof(TNode));
            p->data=ch;p->left=p->right=Null;
            if(b=Null)
                b=p;
            else
             {   switch(k)
                 {
                     case1:St[top]->left=p;break;
                     case2:St[top]->right=p;break;
                 }

             }
             j++;ch=str[j];
        }
    }
}
void DsetoryTree(TNode *&b)
{
    if(b!=Null)
    {    DestoryTree(b->left);
         DestoryTree(b->right);
         free(b);   

    }
}
TNode *FindNode(TNode *b,ElemType x)
{
    TNode *p;
    if(b==Null)
        return Null;
    else if(b->data==x)
        return b;
    else{
        p=FindNode(b->left,x);
        if(p!=Null)
            return P;
        else
            return FindNode(b->right,x)
    }
}
TNode * LeftNode(TNode *p)
{
    return p->left;
}
TNode * RightNode(TNode *p)
{
    return p->right;
}
int TreeHeight(TNode *b)
{
    int leftdh,rightdh;
    if(b==Null)return(0);
    else
    {
        leftdh=TreeHeight(b->left)
        rightdh=TreeHeight(b->right)
        return(leftdh>rightdh)?(leftdh+1):(rightdh+1);
    }
}
void DispBtree(TNode *b)
{
    if(b!=NULL)
    {
        printf("%c",b->data);
        if(b->left!=NULL||b->right!=NULL)
        {
            printf("(");
            DispBTree(b->left);
            if(b->right!=NULL)printf(",");
            DispBtree(b->right);
            printf(")");
        }
    }
}
int main()
{
    TNode *b,*p,*lp,*rp;
    printf("二叉树的基本运算如下：\n");
    printf("(1)创建二叉树\n");
    CreatTree(b,"A(B(D,E(H(J,K(L,M(,N)))),C(F,G(,I)))");
    printf("(2)输出二叉树:")DispBtree(b);printf("\n");
    printf("(3)H结点：")；
    p=FindNode(b,"H");
    if(p!=NULL)
    {
        lp=LeftNode(p);
        if(lp!=NULL)printf("左孩子为%c",lp->data);
        else printf("无左孩子")；
        rp=RightNode(p);
        if(rp!=NULL) printf("右孩子为%c",rp->data);
        else printf("无右孩子");
    }
    printf("\n");
    printf("(4)二叉树b的高度：%d\n",TreeHeight(b));
    prinf("(5)释放二叉树b\n");
    DsetoryTree(b);
    return 1;
}